from data_processing import Data
import simulink

sim = simulink.simulink()


class links(Data):
    def __init__(self, data, i):
        self.t = 0
        self.data = data.strip()
        self.symbol = ''
        self.mark = ''
        self.val = []
        self.f = ''
        self.i = i
        self.out = 0
        self.lock = 0
        self.lock_val = 0
        self.excep = 0
        self.begin()
        self.two_lock = 0
        self.two_lock_val = 0
        self.three_lock = 0
        self.three_lock_val = 0
        self.diff = 0
        self.out_out = ''

    def T(self):
        if self.out in sim:
            q = int(self.out)
            # print(f"{q}端口")
            q += 1
            if str(q) in sim:
                self.t = 1
            else:
                q += 1
                if str(q) in sim:
                    self.t = 2
                else:
                    self.t = 3
        else:
            print(f"{self.mark}未找到端口")
            self.excep = 16

    def readout(self):
        if self.excep != 0:
            print(f"第{self.i}行无法进行正常读取simulink数据，错误： readout() in links.py")
            return
        if self.t == 1:
            if self.lock == '1':
                print(f"{self.i}行{self.mark}值已被锁定，错误：readout() in links.py")
                pass
            elif self.lock == '0':
                self.read_simulink1()
        elif self.t == 2:
            self.read_simulink2()
        elif self.t == 3:
            self.read_simulink3()
        else:
            print(f"{self.mark}读取模态错误")
            self.excep = 15

    def read_simulink1(self):
        li = sim[self.out]
        if self.f == '=':
            list1 = self.eq(li)
        elif self.f == '!=':
            list1 = self.eq(li)
            list2 = {'0', '1'}
            list1 = list2 - list1
        elif self.f == '<':
            list1 = self.less(li)
        elif self.f == '>':
            list1 = self.greater(li)
        elif self.f == '≤':
            list1 = self.less_eq(li)
        elif self.f == '≥':
            list1 = self.greater_eq(li)
        else:
            print(f"{self.i}行无法处理数据{self.mark},位于links.py read_simulink1()")
            self.excep = 13
            list1 = set()
        self.one(list1)

    def read_simulink2(self):
        li = sim[self.out]
        if self.f == '=':
            list1 = self.eq(li)
        elif self.f == '<':
            list1 = self.less(li)
        elif self.f == '>':
            list1 = self.greater(li)
        elif self.f == '≤':
            list1 = self.less_eq(li)
        elif self.f == '≥':
            list1 = self.greater_eq(li)
        elif self.f == '!=':
            list2 = {'01', '00', '11', '10'}
            list1 = self.eq(li)
            list1 = list2 - list1
        else:
            print(f"{self.i}行无法处理数据{self.mark},位于links.py read_simulink2()")
            print(f"四态端口还未完善，错误在{self.i}行 {self.mark}")
            self.excep = 13
            list1 = set()
        self.four(list1)

    def read_simulink3(self):
        li = sim[self.out]
        if self.f == '=':
            list1 = self.eq(li)
        elif self.f == '!=':
            list1 = self.eq(li)
            list2 = {'0x1', '111', '101', '110', '0x0', '100'}
            list1 = list2 - list1
        elif self.f == '<':
            list1 = self.less(li)
        elif self.f == '>':
            list1 = self.greater(li)
        elif self.f == '≤':
            list1 = self.less_eq(li)
        elif self.f == '≥':
            list1 = self.greater_eq(li)
        else:
            print(f"{self.i}行无法处理数据{self.mark},位于links.py read_simulink3()")
            print(f"六态端口还未完善，错误在{self.i}行 {self.mark}")
            self.excep = 13
            list1 = set()
        self.six(list1)

    def eq(self, li) -> set:
        list1 = set()
        for val in self.val:
            val1 = float(val)
            val2 = int(val)
            val1 = str(val1)
            val2 = str(val2)
            if val1 in li:
                list1.add(li[val1])
            if val2 in li:
                list1.add(li[val2])
        return list1

    def less(self, li) -> set:
        key = li.keys()
        list1 = set()
        for k in key:
            k = int(k)
            if k < float(self.val[0]):
                na = li[str(k)]
                list1.add(na)
        return list1

    def less_eq(self, li) -> set:
        key = li.keys()
        list1 = set()
        for k in key:
            k = int(k)
            if k <= float(self.val[0]):
                na = li[str(k)]
                list1.add(na)
        return list1

    def greater(self, li) -> set:
        key = li.keys()
        list1 = set()
        for k in key:
            k = int(k)
            if k > float(self.val[0]):
                na = li[str(k)]
                list1.add(na)
        return list1

    def greater_eq(self, li) -> set:
        key = li.keys()
        list1 = set()
        for k in key:
            k = int(k)
            if k >= float(self.val[0]):
                na = li[str(k)]
                list1.add(na)
        return list1

    def one(self, list1):
        if len(list1) == 1:
            na = list1.pop()
            self.lock = 1
            self.lock_val = na
            self.diff = 1
            self.diff_out(self.lock_val, 0, 0, 0, 0, 0)
        elif len(list1) == 2:
            self.lock = 0
            self.lock_val = 0
        else:
            print(f"{self.i}行存在异常，位于links.py ,def one(),len长度错误，不处于1-2内任何值")
            print(f"len长度为{len(list1)}")
            self.excep = 12

    def four(self, list1):
        if len(list1) == 1:
            na = list1.pop()
            if na == '11':
                self.two_lockval(1, 1, 1, 1)
                self.diff = 2
                self.diff_out(1, 1, 0, 0, 0, 0)
            elif na == '10':
                self.two_lockval(1, 1, 1, 0)
                self.diff = 2
                self.diff_out(1, 0, 0, 0, 0, 0)
            elif na == '01':
                self.two_lockval(1, 0, 1, 1)
                self.diff = 2
                self.diff_out(0, 1, 0, 0, 0, 0)
            elif na == '00':
                self.two_lockval(1, 0, 1, 0)
                self.diff = 2
                self.diff_out(0, 0, 0, 0, 0, 0)
            else:
                print(f"{self.i}行发生错误，位于links.py ,def four(),len == 1")
        elif len(list1) == 2:
            if '11' in list1 and '01' in list1:
                self.two_lockval(0, 0, 1, 1)
                self.diff = 4
                self.diff_out(1, 1, 0, 1, 0, 0)
            elif '11' in list1 and '10' in list1:
                self.two_lockval(1, 1, 0, 0)
                self.diff = 4
                self.diff_out(1, 1, 1, 0, 0, 0)
            elif '11' in list1 and '00' in list1:
                self.two_lockval(0, 0, 0, 0)
                self.diff = 4
                self.diff_out(1, 1, 0, 0, 0, 0)
            elif '01' in list1 and '10' in list1:
                self.two_lockval(0, 0, 0, 0)
                self.diff = 4
                self.diff_out(0, 1, 1, 0, 0, 0)
            elif '01' in list1 and '00' in list1:
                self.two_lockval(1, 0, 0, 0)
                self.diff = 4
                self.diff_out(0, 1, 0, 0, 0, 0)
            elif '10' in list1 and '00' in list1:
                self.two_lockval(0, 0, 1, 0)
                self.diff = 4
                self.diff_out(1, 0, 0, 0, 0, 0)
            else:
                print(f"{self.i}行发生错误，位于links.py ,def four(),len == 2")
        elif len(list1) == 3:
            self.two_lockval(0, 0, 0, 0)
            self.diff = 3
            if '11' not in list1:
                self.diff_out(1, 1, 0, 0, 0, 0)
            elif '10' not in list1:
                self.diff_out(1, 0, 0, 0, 0, 0)
            elif '01' not in list1:
                self.diff_out(0, 1, 0, 0, 0, 0)
            elif '00' not in list1:
                self.diff_out(0, 0, 0, 0, 0, 0)
            else:
                print(f"{self.i}行发生错误，位于links.py ,def four(),len == 3")
        elif len(list1) == 4:
            self.two_lockval(0, 0, 0, 0)
        else:
            print(f"{self.i}行存在异常，位于links.py ,def four(),len长度错误，不处于1-4内任何值")
            print(f"len长度为{len(list1)}")
            self.excep = 11

    def six(self, list1):
        if len(list1) == 1:
            na = list1.pop()
            if na == '0x1':
                self.three_lockval(1, 0, 0, 0, 1, 1)
                self.diff = 6
                self.diff_out(0, 0, 1, 0, 0, 0)
            elif na == '111':
                self.three_lockval(1, 1, 1, 1, 1, 1)
                self.diff = 5
                self.diff_out(1, 1, 1, 0, 0, 0)
            elif na == '101':
                self.three_lockval(1, 1, 1, 0, 1, 1)
                self.diff = 5
                self.diff_out(1, 0, 1, 0, 0, 0)
            elif na == '0x0':
                self.three_lockval(1, 0, 0, 0, 1, 0)
                self.diff = 6
                self.diff_out(0, 0, 0, 0, 0, 0)
            elif na == '110':
                self.three_lockval(1, 1, 1, 1, 1, 0)
                self.diff = 5
                self.diff_out(1, 1, 0, 0, 0, 0)
            elif na == '100':
                self.three_lockval(1, 1, 1, 0, 1, 0)
                self.diff = 5
                self.diff_out(1, 0, 0, 0, 0, 0)
            else:
                print(f"{self.i}行发生错误，位于links.py ,def six(),len == 1")
        elif len(list1) == 2:
            self.treat(list1)
        elif len(list1) == 3:
            self.treat(list1)
        elif len(list1) == 4:
            self.treat(list1)
        elif len(list1) == 5:
            self.treat(list1)
        elif len(list1) == 6:
            self.treat(list1)
        else:
            print(f"{self.i}行存在异常，位于links.py ,def six(),len(list1)长度错误，不处于1-6内任何值")
            self.excep = 10

    def two_lockval(self, x1, y1, x2, y2):
        self.lock = x1
        self.lock_val = y1
        self.two_lock = x2
        self.two_lock_val = y2

    def three_lockval(self, x1, y1, x2, y2, x3, y3):
        self.lock = x1
        self.lock_val = y1
        self.two_lock = x2
        self.two_lock_val = y2
        self.three_lock = x3
        self.three_lock_val = y3

    def diff_out(self, x1, y1, x2, y2, x3, y3):
        if self.diff == 4:      # 读取四位
            self.out_out = '((out('+str(self.out)+')=='+str(x1)+'&&out('+str(int(self.out)+1)+')=='+str(y1)+')'
            self.out_out = self.out_out+'||(out('+str(self.out)+')=='+str(x2)+'&&out('+str(int(self.out)+1)+')=='+str(y2)+'))'
        elif self.diff == 1:    # 读取一位
            self.out_out = '(out('+str(self.out)+')=='+str(x1)+')'
        elif self.diff == 2:    # 读取两位
            self.out_out = '(out('+str(self.out)+')=='+str(x1)+'&&out('+str(int(self.out)+1)+')=='+str(y1)+')'
        elif self.diff == 3:    # 读取～ 两位
            self.out_out = '(~(out('+str(self.out)+')=='+str(x1)+'&&out('+str(int(self.out)+1)+')=='+str(y1)+'))'
        elif self.diff == 5:    # 读取三位
            self.out_out = '(out('+str(self.out)+')=='+str(x1)+'&&out('+str(int(self.out)+1)+')=='+str(y1)
            self.out_out = self.out_out +'&&out('+str(int(self.out)+2)+')=='+str(x2)+')'
        elif self.diff == 6:        # 读取三位，中间位为空
            self.out_out = '(out('+str(self.out)+')=='+str(x1)+'&&out('+str(int(self.out)+2)+')=='+str(x2)+')'

    def printf(self):
        if self.excep == 0:
            print(self.symbol, self.mark, self.f, self.val, self.out)
            if self.t == 1:
                print(f"mark({self.out},:)\t=[{self.lock},{self.lock_val}]")
            elif self.t == 2:
                print(f"mark({self.out},:)\t=[{self.lock},{self.lock_val}]")
                print(f"mark({int(self.out)+1},:)\t=[{self.two_lock},{self.two_lock_val}]")
            elif self.t == 3:
                print(f"mark({self.out},:)\t=[{self.lock},{self.lock_val}]")
                print(f"mark({int(self.out) + 1},:)\t=[{self.two_lock},{self.two_lock_val}]")
                print(f"mark({int(self.out) + 2},:)\t=[{self.three_lock},{self.three_lock_val}]")
            print(self.out_out)
        else:
            print(self.data)
            print(self.symbol, self.mark, self.f, self.val, self.out)
            print(f"最后出现的错误编码{self.excep}")

    def treat(self, list1):
        x = set()
        y = set()
        z = set()
        for na in list1:
            if na == '110':
                x.add(1)
                y.add(1)
                z.add(0)
                lis = f"(out({self.out})==1&&out({str(int(self.out)+1)})==1&&out({str(int(self.out)+2)})==0)"
            elif na == '100':
                x.add(1)
                y.add(0)
                z.add(0)
                lis = f"(out({self.out})==1&&out({str(int(self.out) + 1)})==0&&out({str(int(self.out) + 2)})==0)"
            elif na == '111':
                x.add(1)
                y.add(1)
                z.add(1)
                lis = f"(out({self.out})==1&&out({str(int(self.out) + 1)})==1&&out({str(int(self.out) + 2)})==1)"
            elif na == '0x1':
                x.add(0)
                z.add(1)
                lis = f"(out({self.out})==0&&out({str(int(self.out) + 2)})==1)"
            elif na == '0x0':
                x.add(0)
                z.add(0)
                lis = f"(out({self.out})==0&&out({str(int(self.out) + 2)})==0)"
            elif na =='101':
                x.add(1)
                y.add(0)
                z.add(1)
                lis = f"(out({self.out})==1&&out({str(int(self.out) + 1)})==0&&out({str(int(self.out) + 2)})==1)"
            else:
                lis = ''
                print(f"{self.i}行存在异常，位于links.py ,def treat(),读取simulink错误，未读取到正常参数")
            if self.out_out != '':
                self.out_out = self.out_out + '||'
            self.out_out = self.out_out + lis
        self.out_out = '(' + self.out_out + ')'
        if len(x) == 0:
            self.lock = 0
            self.lock_val = 0
            print(f"{self.i}行存在异常，位于links.py ,def treat(),len(x)长度错误，不处于1-2内任何值")
        elif len(x) == 1:
            self.lock = 1
            self.lock_val = x.pop()
        elif len(x) == 2:
            self.lock = 0
            self.lock_val = 0
        if len(y) == 0 or len(y) == 2:
            self.two_lock = 0
            self.two_lock_val = 0
        elif len(y) == 1:
            self.two_lock = 1
            self.two_lock_val = y.pop()
        if len(z) == 1:
            self.three_lock = 1
            self.three_lock_val = z.pop()
        elif len(z) == 2:
            self.three_lock = 0
            self.three_lock_val = 0
        elif len(z) == 0:
            print(f"{self.i}行存在异常，位于links.py ,def treat(),len(z)长度错误，不处于1-2内任何值")

